<template>
  <div class="article-comments">
    <!-- 评论列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <CommentItem @on-click-reply="$emit('on-click-reply', $event)" v-for="item in list"  :key="item.com_id + ''" :comment="item"/>
    </van-list>
    <!-- 评论列表 -->
  </div>
</template>

<script>
import { getCommenList } from '@/api/commen.js'
import CommentItem from '@/components/comment-item.vue'
export default {
  name: 'ArticleComment',
  data() {
    return {
      loading: false, // 上拉加载更多的 loading
      finished: false, // 是否加载结束
      offset: null
    }
  },

  components: {
    CommentItem
  },

  props: {
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Object, Number],
      required: true
    },
    list: {
      type: Array,
      required: true
    }
  },

  methods: {
    async onLoad() {
      // 异步请求
      const res = await getCommenList({
        type: this.type, // a: 文章的评论 // c: 评论的评论
        source: this.source + '', // 文章id 或者 评论id
        offset: this.offset,
        limit: 10
      })
      // console.log(res.data.total_count)
      this.$emit('on-response-success', res.data)
      // 保存数据
      this.list.push(...res.data.results)
      // 加载状态结束
      this.loading = false
      // 数据全部加载完成
      if (res.data.results.length) {
        // 有下一页
        this.offset = res.data.last_id
      } else {
        this.finished = true
      }
    }
  }
}
</script>

<style scoped lang="less">
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}

.van-list {
  margin-bottom: 45px;
}
</style>
